package com.ynding.cloud.auth.server.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynding.cloud.auth.api.admin.domain.UserDO;
import com.ynding.cloud.auth.api.admin.entity.Role;
import com.ynding.cloud.auth.api.admin.entity.User;
import com.ynding.cloud.auth.api.admin.entity.UserRole;
import com.ynding.cloud.auth.server.admin.dao.RoleMapper;
import com.ynding.cloud.auth.server.admin.dao.UserMapper;
import com.ynding.cloud.auth.server.admin.dao.UserRoleMapper;
import com.ynding.cloud.auth.server.admin.service.IUserService;
import com.ynding.cloud.common.model.bo.ResponseBean;
import com.ynding.cloud.common.model.util.BeanDtoVoUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static com.ynding.cloud.common.model.bo.ResponseCode.USER_AlREAD_EXIST_EXCEPTION;

/**
 * <p> </p>
 *
 * @author ynding
 * @version 2021/11/1
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements IUserService {
    private final UserMapper userMapper;
    private final UserRoleMapper userRoleMapper;
    private final RoleMapper roleMapper;

    @Override
    public UserDO loadUserByUsername(String username) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, username));
        return toUserDO(user);
    }

    /**
     * 注册
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean userReg(String username, String password) {
        //如果用户名存在，返回错误
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, username));
        if (user != null) {
            return ResponseBean.fail(USER_AlREAD_EXIST_EXCEPTION);
        }
        user = User.builder()
                .username(username)
                .password(password)
                .build();
        return ResponseBean.ok(userMapper.insert(user));
    }

    /**
     * 根据用户id 查找角色列表
     *
     * @param userid
     * @return
     */
    private List<Role> listRoleByUserid(Long userid) {
        List<UserRole> userRoles = userRoleMapper.selectList(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getUserid, userid));
        List<Role> roles = roleMapper.selectBatchIds(userRoles.stream().map(UserRole::getRid).collect(Collectors.toList()));
        return roles;
    }

    /**
     * 转换成领域类
     *
     * @param user
     * @return
     */
    private UserDO toUserDO(User user) {
        UserDO userDO = BeanDtoVoUtils.toDtoBean(user, UserDO.class);
        List<Role> roles = listRoleByUserid(user.getId());
        userDO.setRoles(roles);
        return userDO;
    }

}
